package com.wondersgroup.insurance.dnuilearn.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/7/3.
 */

public class CameraView2 extends View {
    private  Camera camera;
    private  Matrix matrix;
    private Paint paint;
    Bitmap bitmap;
    public CameraView2(Context context) {
        this(context, null);
    }

    public CameraView2(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CameraView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.GREEN);

        camera = new Camera();
        matrix = new Matrix();

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xyjy2);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        matrix.reset();
        camera.save();
//        camera.rotateX(45);//绕着X轴旋转
        camera.rotateY(-45);
//        camera.rotateZ(45);
        camera.getMatrix(matrix);
        camera.restore();
        //前乘矩阵---先平移矩阵，再进行变换
        matrix.preTranslate(-bitmap.getWidth()/2,-bitmap.getHeight()/2);

//        matrix.preTranslate(bitmap.getWidth()/2,bitmap.getHeight()/2);

        //变换玩之后，再恢复到原来的位置
        matrix.postTranslate(bitmap.getWidth()/2,bitmap.getHeight()/2);
//        matrix.postTranslate(-bitmap.getWidth()/2,-bitmap.getHeight()/2);

        canvas.drawBitmap(bitmap,matrix,paint);

    }
}
